package game1;

import java.io.*;
import java.net.*;

public class Game1Client {
    String host;
    int port;

    public Game1Client(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public int run() {
        String strSocket;
        int ans = 0;
        try {
            Socket socketClient = new Socket(host, port);
            BufferedReader brInFromServer = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
            DataOutputStream dosOutToServer = new DataOutputStream(socketClient.getOutputStream());
            dosOutToServer.writeBytes("hello\n");
            int minimum = Integer.parseInt(brInFromServer.readLine());
            int maximum = Integer.parseInt(brInFromServer.readLine());
            int limit = Integer.parseInt(brInFromServer.readLine());
            int times = 0;
            while (times < limit) {
                times += 1;
                ans = (maximum + minimum) / 2;
                dosOutToServer.writeBytes(String.format("%d\n", ans));
                strSocket = brInFromServer.readLine();
                switch (strSocket) {
                    case "<":
                        minimum = ans;
                        break;
                    case ">":
                        maximum = ans;
                        break;
                }
                if (strSocket.equals("Correct!")) {
                    break;
                }
            }
            dosOutToServer.writeBytes("bye\n");
            socketClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ans;
    }
}
